<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="所属字典"  >
        <el-input v-model="form.dictName" disabled ></el-input>
      </el-form-item>
			<el-form-item label="项名称" prop="dataCode">
				<el-input v-model="form.dataCode" clearable></el-input>
			</el-form-item>
			<el-form-item label="键值" prop="dataValue">
				<el-input v-model="form.dataValue" clearable></el-input>
			</el-form-item>
      <el-form-item label="排序号" prop="sortNum">
        <el-input-number v-model="form.sortNum" clearable></el-input-number>
      </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "create",
				titleMap: {
					add: '新增项',
					edit: '编辑项'
				},
				visible: false,
				isSaveing: false,
				form: {
					id: "",
          dataValue: "",
          dataCode: "",
          dict: "",
          dictName: "",
          sortNum:""
				},
				rules: {
					dic: [
						{required: true, message: '请选择所属字典'}
					],
          dataCode: [
						{required: true, message: '请输入项名称'}
					],
          dataValue: [
						{required: true, message: '请输入键值'}
					]
				},
				dic: [],
				dicProps: {
					value: "id",
					label: "name",
					emitPath: false,
					checkStrictly: true
				}
			}
		},
		mounted() {
			if(this.params){
				this.form.dic = this.params.code
			}
			this.getDic()
		},
		methods: {
			//显示
			 open(dict) {
         let self=this;
        this.form.dict = dict;
        this.visible = true;
         this.$http.form(`/dict/view`, {id:dict}).then(res=>{
           self.form.dictName=res.data.title;
         })

        return this;
      },
			//获取字典列表
			async getDic(){
				//var res = await this.$API.system.dic.tree.get();
				//this.dic = res.data;
			},
			//表单提交方法
			submit(){
        let self=this;
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
            let res = await this.$http.form(`/dictItem/${self.mode}`, this.form);
						this.isSaveing = false;
						if(res.code === 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.msg, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
        this.mode="update";
				this.form.id = data.id
				this.form.dataCode = data.dataCode
				this.form.dataValue = data.dataValue
        this.form.sortNum=data.sortNum;
			}
		}
	}
</script>

<style>
</style>
